package com.qcby.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.entity.TCategory;
import com.qcby.form.TCategoryListForm;
import com.qcby.mapper.TCategoryMapper;
import com.qcby.service.TCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.vo.PageVO;
import com.qcby.vo.TCategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2025-02-04
 */
@Service
public class TCategoryServiceImpl extends ServiceImpl<TCategoryMapper, TCategory> implements TCategoryService {
    @Autowired
    private TCategoryMapper categoryMapper;
    @Override
    public PageVO categoryList(TCategoryListForm categoryListForm) {
        Page<TCategory> page = new Page<>(categoryListForm.getPage(), categoryListForm.getLimit());
        //分页查询  根据小区名称模糊查询
        QueryWrapper<TCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(categoryListForm.getCategoryName()),"category_name", categoryListForm.getCategoryName());
        Page<TCategory> resultPage = this.categoryMapper.selectPage(page, queryWrapper);
        PageVO pageVO = new PageVO();
        List<TCategoryVo> list = new ArrayList<>();
        for (TCategory record : resultPage.getRecords()) {
            TCategoryVo categoryVo = new TCategoryVo();
            BeanUtils.copyProperties(record, categoryVo);
            list.add(categoryVo);
        }
        pageVO.setList(list);
        pageVO.setTotalCount(resultPage.getTotal());
        pageVO.setPageSize(resultPage.getSize());
        pageVO.setCurrPage(resultPage.getCurrent());
        pageVO.setTotalPage(resultPage.getPages());
        return pageVO;
    }
}
